/*	Author		: Arga
 *	Created On	: Apr 25, 2011
 *	Description	: 
 */

package team8.paacdroid.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import team8.paacdroid.R;
import team8.paacdroid.bean.BusRide;
import android.content.Context;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class BusRideAdapter extends ArrayAdapter<BusRide> {
	private ArrayList<BusRide> items;

	public BusRideAdapter(Context context, int textViewResourceId, ArrayList<BusRide> items) {
		super(context, textViewResourceId, items);
		this.items = items;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		
		// Pop up the view
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.bus_ride_item, null);
		}
		
		// Get the itinerary to display
		BusRide i = items.get(position);
		if (i != null) {
			// Get the widgets
			TextView tvRouteNo = (TextView) v.findViewById(R.id.tvRouteNo);
			TextView tvRouteNameAndDirection = (TextView) v.findViewById(R.id.tvRouteNameAndDirection);
			TextView tvBoardInstruction = (TextView) v.findViewById(R.id.tvBoardInstruction);
			TextView tvOffInstruction = (TextView) v.findViewById(R.id.tvOffInstruction);
			TextView tvHasDetours = (TextView) v.findViewById(R.id.tvHasDetours);
			
			// Get & display the route #
			tvRouteNo.setText(i.getRouteNo() != null ? i.getRouteNo() : "");
			
			// Get & display the route name & direction
			tvRouteNameAndDirection.setText(i.getRouteName() != null ? i.getRouteName() : "");
			tvRouteNameAndDirection.setText(i.getDirection() != null ? tvRouteNameAndDirection.getText() + " - " + i.getDirection() : tvRouteNameAndDirection.getText());

			// Get & display the board instruction, location, and time
			tvBoardInstruction.setText(i.getBoardInstruction() != null ? i.getBoardInstruction() : "");
			tvBoardInstruction.setText(i.getBoardStopName() != null ? tvBoardInstruction.getText() + " " + i.getBoardStopName() : i.getBoardInstruction());
			tvBoardInstruction.setText(i.getBoardTime() != null ? tvBoardInstruction.getText() + " at " + new SimpleDateFormat("h:mm aa").format(i.getBoardTime()) : i.getBoardInstruction());

			// Get & display the off instruction, location, and time
			tvOffInstruction.setText(i.getOffInstruction() != null ? i.getOffInstruction() : "");
			tvOffInstruction.setText(i.getOffStopName() != null ? tvOffInstruction.getText() + " " + i.getOffStopName() : i.getOffInstruction());
			tvOffInstruction.setText(i.getOffTime() != null ? tvOffInstruction.getText() + " at " + new SimpleDateFormat("h:mm aa").format(i.getOffTime()) : i.getOffInstruction());
			
			// TODO Find out if this bus has detours. If so, display a notification.			
			
		}

		// Return
		return v;
	}
}
